package org.zoomdev.zoom.dao.adapters.mapper;

import org.zoomdev.zoom.common.utils.Classes;
import org.zoomdev.zoom.dao.adapters.ResultSetAdapter;
import org.zoomdev.zoom.dao.adapters.ResultSetMapper;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;

public class EntityMapper<T> implements ResultSetMapper<T> {

    private final Field field;

    private final ResultSetAdapter adapter;

    public EntityMapper(Field field, ResultSetAdapter adapter) {
        this.field = field;
        this.adapter = adapter;
    }

    @Override
    public void map(ResultSet rs, int columnIndex, T data) throws SQLException {
        Object value = adapter.getObject(rs, columnIndex);
        Classes.set(field, data, value);
    }
}
